package com.ruoyi.project.doctors.serivce.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.doctors.domain.*;
import com.ruoyi.project.doctors.domain.dot.SchedulingFormDataDto;
import com.ruoyi.project.doctors.domain.dot.SchedulingFormDto;
import com.ruoyi.project.doctors.domain.dot.SchedulingRespDto;
import com.ruoyi.project.doctors.domain.dot.SchedulingSelectDot;
import com.ruoyi.project.doctors.mapper.DoctSchedulingMapper;
import com.ruoyi.project.doctors.serivce.DoctSchedulingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

@Service
public class DoctSchedulingServiceImpl implements DoctSchedulingService {

    @Autowired
    private DoctSchedulingMapper doctSchedulingMapper;

    @Override
    public List<UserDoc> selectUserByDeptid(Long deptId) {
        List<UserDoc> users = doctSchedulingMapper.selectUserByDeptId(deptId);
        return users;
    }

    @Override
    public List<DeptDoc> selectDeptByDeptId() {
        return doctSchedulingMapper.selectDeptByDeptId();
    }

    @Override
    public Map<String, Object> getSchedulingData(SchedulingRespDto schedulingRespDto) {
        // 获取当前时间
        DateTime date = DateUtil.date();
        // 判断传来的值具体是哪一天，然后设置date为当前日期，下一周的周一（点击下一周按钮），上一周的周日（点击上一周按钮）
        if(StringUtils.isNotBlank(schedulingRespDto.getQueryDate())){
            // 如果不为空，说明传来的是周一的日期或周日的日期
            String queryDate = schedulingRespDto.getQueryDate();
            date = DateUtil.parse(queryDate, "yyyy-MM-dd");
            // 判断当前日期是周一还是周日
            int i = DateUtil.dayOfWeek(date); // 获取日期对应的星期几，1表示周日，2表示周期
            if(i==1){
                // 获取周日的后一天，即下一周的周一,
                date = DateUtil.offsetDay(date, 1);
            }else{
                date = DateUtil.offsetDay(date,-1);
            }
        }

        // 获取到date以后，根据date获取日期表开始时间和结束时间
        DateTime beginDate = DateUtil.beginOfWeek(date); // 根据date获取到开始日期
        DateTime endDate = DateUtil.endOfWeek(date); // 根据date获取结束日期
        // map封装
        Map<String, String>  schedulingData= new HashMap<>();
        schedulingData.put("startTimeThisWeek",DateUtil.format(beginDate,"yyyy-MM-dd")+"(周一)");
        schedulingData.put("endTimeThisWeek",DateUtil.format(endDate,"yyyy-MM-dd")+"(周日)");

        // 根据开始日期获取本周所有的日期
        List<String> datesOfWeek = getDatesOfWeek(beginDate);
        // 将日期转化为指定格式
        String[] labelNames = new String[7];
        for(int i=0;i<=6;i++){
           labelNames[i] = datesOfWeek.get(i) + turnChineseWeek(i);
        }
        // 根据datesOfWeek获取数据库中数据
        SchedulingSelectDot schedulingSelectDot = new SchedulingSelectDot(datesOfWeek, schedulingRespDto.getUserId(), schedulingRespDto.getDeptId());
        List<SchedulingWeeks> schedulingWeeks = doctSchedulingMapper.selectSchedulingData(schedulingSelectDot);
        System.out.println("数据库中数据：" + schedulingWeeks);

        ArrayList<Object> tableData = new ArrayList<>();
        for (SchedulingWeeks schedulingWeek : schedulingWeeks) {
            Map<String, Object> map = new HashMap<>();
            map.put("userId",schedulingWeek.getUserId());
            map.put("deptId",schedulingWeek.getDeptId());
            map.put("subsectionType",schedulingWeek.getSubsectionType());
            ArrayList<String> schedulingType = new ArrayList<>();
            schedulingType.add(StringUtils.substring(schedulingWeek.getDay01SchedulingType(),0,1));
            schedulingType.add(StringUtils.substring(schedulingWeek.getDay02SchedulingType(),0,1));
            schedulingType.add(StringUtils.substring(schedulingWeek.getDay03SchedulingType(),0,1));
            schedulingType.add(StringUtils.substring(schedulingWeek.getDay04SchedulingType(),0,1));
            schedulingType.add(StringUtils.substring(schedulingWeek.getDay05SchedulingType(),0,1));
            schedulingType.add(StringUtils.substring(schedulingWeek.getDay06SchedulingType(),0,1));
            schedulingType.add(StringUtils.substring(schedulingWeek.getDay07SchedulingType(),0,1));

//            String[] SchedulingType= new String[7];
//            SchedulingType[0] = schedulingWeek.getDay01SchedulingType();
//            SchedulingType[1] = schedulingWeek.getDay02SchedulingType();
//            SchedulingType[2] = schedulingWeek.getDay03SchedulingType();
//            SchedulingType[3] = schedulingWeek.getDay04SchedulingType();
//            SchedulingType[4] = schedulingWeek.getDay05SchedulingType();
//            SchedulingType[5] = schedulingWeek.getDay06SchedulingType();
//            SchedulingType[6] = schedulingWeek.getDay07SchedulingType();
            map.put("schedulingType",schedulingType);
            tableData.add(map);
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("labelNames",labelNames);
        map.put("schedulingData",schedulingData);
        map.put("tableData",tableData);

        System.out.println("returnData:" + map);
        return map;
    }


    /**
     * 新增数据
     * @param schedulingFormDto
     * @return
     */
    @Override
    public int insertScheduling(SchedulingFormDto schedulingFormDto) {
        int count = 0;
        if(null!= schedulingFormDto.getData() && schedulingFormDto.getData().size()>0){
            String beginDate = schedulingFormDto.getBeginDate();
            DateTime beginOfWeek = DateUtil.parseDate(beginDate);
            DateTime endOfWeek = DateUtil.endOfWeek(beginOfWeek);
            String startWeek = DateUtil.format(beginOfWeek, "yyyy-MM-dd");
            String endWeek = DateUtil.format(endOfWeek, "yyyy-MM-dd");
            SchedulingFormDataDto schedulingFormDataDto = schedulingFormDto.getData().get(0);
            Integer userId = schedulingFormDataDto.getUserId();
            if(null!=userId){
                doctSchedulingMapper.deleteScheduling(startWeek, endWeek, userId);
                List<String> datesOfWeek = getDatesOfWeek(beginOfWeek);
                List<SchedulingFormDataDto> data = schedulingFormDto.getData();
                for (SchedulingFormDataDto datum : data) {
                    List<String> schedulingType = datum.getSchedulingType();
                    Scheduling scheduling = new Scheduling();
                    for(int i=0;i<schedulingType.size();i++){
                        if(StringUtils.isNotBlank(schedulingType.get(i))){
                            scheduling.setUserId(datum.getUserId());
                            scheduling.setDeptId(datum.getDeptId());
                            scheduling.setSchedulingDay(datesOfWeek.get(i));
                            scheduling.setSubsectionType(datum.getSubsectionType());
                            scheduling.setSchedulingType(schedulingType.get(i));
                            count = doctSchedulingMapper.insertScheduling(scheduling);
                        }
                    }
                }
            }
        }
        return count;
    }

    /**
     * 新增数据
     * @param scheduling
     * @return
     */
    @Override
    public int insertSchedulingNew(Scheduling scheduling) {
        String schedulingDay = scheduling.getSchedulingDay();
        DateTime dateTime = DateUtil.parseDate(schedulingDay);
        scheduling.setSchedulingDay(DateUtil.format(dateTime,"yyyy-MM-dd"));
        return doctSchedulingMapper.insertScheduling(scheduling);
    }




    // 获取排班的日期，同时日期后面添加星期几
    private List<String>  getDatesOfWeek(DateTime beginDate) {
        ArrayList<String> weekDays = new ArrayList<>();
        for(int i=0;i<=6;i++){
            DateTime dateTime = DateUtil.offsetDay(beginDate, i);
            weekDays.add(DateUtil.format(dateTime, "yyyy-MM-dd"));
        }
        return weekDays;
    }

    // 翻译“周”
    private String turnChineseWeek(int i){
        switch (i){
            case 0:
                return "(周一)";
            case 1:
                return "(周二)";
            case 2:
                return "(周三)";
            case 3:
                return "(周四)";
            case 4:
                return "(周五)";
            case 5:
                return "(周六)";
            default:
                return "(周日)";
        }
    }





}
